﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WAD.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace WAD.DAL
{
    /// <summary>
    /// 频道数据库操作类
    /// </summary>
    public class DaoChannel : DAO
    {
        /// <summary>
        /// 添加频道
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static bool Add(MChannel m)
        {
            String sql = "insert into channel(name)values(@name)";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@name", DbType.String, m.name);
            return db.ExecuteNonQuery(cmd) > 0;
        }

        /// <summary>
        /// 获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static MChannel GetEntity(int id)
        {
            String sql = "select id,name from channel where id = " + id;
            object obj = BindEntity(sql, new MChannel());
            return obj == null ? null : (MChannel)obj;
        }

        /// <summary>
        /// 删除频道
        /// </summary>
        /// <param name="id">广告id</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public static bool Delete(int id)
        {
            String sql = "delete channel where id = " + id;
            return ExecuteNonQuery(sql) > 0;
        }

        /// <summary>
        /// 隐藏频道
        /// </summary>
        /// <param name="id">广告id</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public static bool Hide(int id)
        {
            String sql = "update channel where id = " + id;
            return ExecuteNonQuery(sql) > 0;
        }

        /// <summary>
        /// 更新频道
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static bool UpdateName(MChannel m)
        {
            String sql = "update channel set name = @name where id = @id";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@name", DbType.String, m.name);
            db.AddInParameter(cmd, "@id", DbType.Int32, m.id);
            return db.ExecuteNonQuery(cmd) > 0;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页显示的总条数</param>
        /// <returns></returns>
        public static PageData GetList(int pageIndex, int pageSize, bool isChache = false)
        {
            String fieldList = "id,name,order_num,'' as url";
            String orderBy = "id";
            String where = "state = 1";
            String tableName = "channel";
            if (isChache)
            {
                PageData pg = (PageData)QD.Cache.Caching.Get("Channel_GetList");
                if (pg == null)
                {
                    pg = GetPageList(fieldList, orderBy, pageIndex, pageSize, where, tableName);
                    QD.Cache.Caching.Set("C_Channel_GetList", pg); //设置缓存
                }
                return pg;
            }
            return GetPageList(fieldList, orderBy, pageIndex, pageSize, where, tableName);
        }

        /// <summary>
        /// 获取默认的频道
        /// </summary>
        /// <returns></returns>
        public static List<Dictionary<string, object>> GetDefaultList()
        {
            String sql = "select top 10 id,name from channel where [state] = 1 order by order_num,id";
            return GetList(sql);
        }

    }
}
